#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：back 
@File    ：train_svc.py
@IDE     ：PyCharm 
@Author  ：七分是你
@Date    ：2024/8/12 下午9:39 
@explain : 
"""
import math

from app import utils, dao


class TrainService:
    @classmethod
    def get_train_list(cls, key_type: str = '', keyword: str = '',
                       page: int = 1,
                       page_size: int = 10, ) -> utils.HttpResponse:
        where = {}
        if key_type != '' and keyword != '':
            where[key_type] = '%' + keyword + '%'
        total, result = dao.TrainQueryDao.list(page=page, page_size=page_size, **where)
        res = {'items': [], 'total': total, 'current_page': page, 'pagesize': page_size,
               'total_page': math.ceil(total / page_size)}
        if total == 0:
            return utils.ResponseSuccess(res)
        # 格式化数据
        records_list = []
        for record in result:
            parent_records = {
                'id': record.id,
                'title': record.title,
                'video': record.video,
                'addtime': utils.StringUtil.format_timestamp_to_str(record.addtime),
            }
            records_list.append(parent_records)
        res = {'items': records_list, 'total': total, 'current_page': page, 'pagesize': page_size,
               'total_page': math.ceil(total / page_size)}
        return utils.ResponseSuccess(res)

    @classmethod
    def get_train_detail(cls, train_id: int) -> utils.HttpResponse:
        where = {'id': train_id}
        res = dao.TrainQueryDao.find(**where)
        records = {
            'id': res.id,
            'title': res.title,
            'video': res.video,
            'addtime': utils.StringUtil.format_timestamp_to_str(res.addtime),
        }
        return utils.ResponseSuccess(records)
